<?php 
	class CommentsController extends CustomControllerAction 
	{
		public function init() 
		{
			parent::init();
		}
		
		public function indexAction()
		{
			$this->_forward('browse');
		}
		public function browseAction() 
		{
			$comment_sdb_mgr = new CommentSDBManager();
			$usr_sdb_mgr = new UserSDBManager();
			$thumb_mgr = new ThumbnailManager();

			$desired_thumb_width = 64;
			$user = $this->getRequest()->getParam('user');
			$dates = CommonGenerator::generateCurrentIso8601Dates();
			
			if ($user) 
			{
				$comments_array = array();

				$users = $usr_sdb_mgr->getByFieldValue('username', $user);
				
				if (sizeof($users) > 0)
					$query_str = "['user_id' = '{$users[0]->getId()}'] intersection ['date' > '0']  sort 'date' desc";
				else
					$query_str = "['date' > '0']  sort 'date' desc";
			}
			else
			{
				$query_str = "['date' > '0']  sort 'date' desc";
			}

			$comments = $comment_sdb_mgr->query($query_str, '5');

			foreach ($comments as $comment) 
			{ 
				$media = new Media();
				$media->load($comment->getAttribute('photo_id')); // <- need to change photo_id to media_id in db
				
				$current_user = new User();	
				$current_user->load($comment->getAttribute('user_id'));

				$comment_attributes = $comment->getAttributes();
				$comment_attributes[username] = $current_user->getAttribute('username');
				$comment_attributes[media_url] = $media->getAttribute('url');
				$comment_attributes[media_thumbnail_url] = $thumb_mgr->getThumbnail($media->getId(), $desired_thumb_width);
				$comments_array[] = $comment_attributes;
			} 

			# set up left tabs	
			if ($user)
			{
				$left_tabs = array(
					'Media' => array('/media/' . $user),
					'Comments' => array('/comments/' . $user)
				);

				$this->view->badge_title = $users[0]->getAttribute('username') . "'s comments";
				
				# check if following this user, and set this
				$this->view->following = null;
			}
			else
			{
				$left_tabs = array(
					'Media' => array('/media'),
					'Comments' => array('/comments')
				);
			}
			
			# set up right tabs
			$auth = Zend_Auth::getInstance();
			if ($auth->hasIdentity())
			{
				$right_tabs = array(
					'Home' => array('/home'),
					'Control Panel' => array('/cpanel')
				);
			}
			else
			{
				$right_tabs = array(
					'Login' => array('/login')
				);
			}
			
			$this->view->tabs = array('selected' => 'Comments', 'left_tabs' => $left_tabs, 'right_tabs' => $right_tabs);
			$this->view->comments = $comments_array;
		}
	}
